package com.vng.zini.gifapp.components;

import java.io.IOException;
import java.util.List;

import com.vng.zini.gifapp.config.AppParameters;

import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MyCameraSurfaceView extends SurfaceView implements
		SurfaceHolder.Callback {

	private SurfaceHolder mHolder;
	private Camera mCamera;

	@SuppressWarnings("deprecation")
	public MyCameraSurfaceView(Context context, Camera camera) {
		super(context);
		mCamera = camera;
		System.out.println("surface contructor");
		// Install a SurfaceHolder.Callback so we get notified when the
		// underlying surface is created and destroyed.
		mHolder = getHolder();
		mHolder.addCallback(this);
		// deprecated setting, but required on Android versions prior to 3.0
		mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int weight,
			int height) {
		// If your preview can change or rotate, take care of those events
		// here.
		// Make sure to stop the preview before resizing or reformatting it.
		System.out.println("surface changed");
		if (mHolder.getSurface() == null) {
			// preview surface does not exist	
			return;
		}

		// stop preview before making changes
		try {
			mCamera.stopPreview();
		} catch (Exception e) {
			// ignore: tried to stop a non-existent preview
		}

		// make any resize, rotate or reformatting changes here
		
		 Camera.Parameters parameters = mCamera.getParameters();
		 
		 AppParameters.listSize = parameters.getSupportedPreviewSizes(); 
		 
		 /*previewSizes.get(0); parameters.setPreviewSize(previewSize.width,
		 previewSize.height); mCamera.setParameters(parameters);*/
		 
		// start preview with new settings
		try {
			mCamera.setPreviewDisplay(mHolder);
			mCamera.startPreview();

		} catch (Exception e) {
		}
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// The Surface has been created, now tell the camera where to draw
		// the preview.
		try {
			System.out.println("surface created");
			mCamera.setPreviewDisplay(holder);
			mCamera.startPreview();
		} catch (IOException e) {
		}
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		System.out.println("surface destroyed");
		//this.getHolder().removeCallback(this);
		//mCamera.release();

	}
}